/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.dep.conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author ppratsc
 */
public class DBWRAP {

    private Connection conn = null;

    public DBWRAP(){
        
    }
    public DBWRAP(Connection cox){
        conn = cox;
    }

    public int insert(String sql) throws SQLException {
        Connection c = getConnection();
        ResultSet rs = null;

        Statement st = c.createStatement();
        st.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
        rs = st.getGeneratedKeys();
        if(rs.next()){
            return rs.getInt(1);
        }
      return 0;
    }
    
    protected static String url = "jdbc:mysql://localhost:3306/caintra?" + "user=root&password=admin";

    private Connection InitConnection()
    {
           Context initContext;
           Context envContext;
           DataSource ds;
           try
           {
                //Intenta Obtener una Conexion del Pool
                initContext = new InitialContext();
                envContext  = (Context)initContext.lookup("java:/comp/env");
                ds = (DataSource)envContext.lookup("jdbc/sapradb");
                conn = ds.getConnection();

           }
           catch (Exception e)
           {
               String ex = e.toString();
               e.printStackTrace();
               try
               {
                    //No existe un Pool de Conexiones
                    //Generamos una Conexion Nueva
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    conn = (Connection) DriverManager.getConnection(url);
                    //WARNING QUE NO SE ESTA UTILIZANDO EL POOL DE CONEXIONES
                    System.out.print("WARNING: NO SE ESTA UTILIZANDO UN POOL DE JDBC");
               }catch (Exception e1) {
                    e1.printStackTrace();
                    return null;
               }
               
           }

           return conn;

    }

    public Connection getConnection()
    {
           if(conn == null)
               return InitConnection();
           else
               return conn;
    }

    public ResultSet select(String sql) throws SQLException
    {   
        Connection c = getConnection();
        ResultSet rs = null;

        Statement st = c.createStatement();
        st.execute(sql);
        rs = st.getResultSet();
        return rs;
    }

     public void update(String sql) throws SQLException
    {
        Connection c = getConnection();
        Statement st = c.createStatement();
        st.executeUpdate(sql);
    }

     public void close(){
         try {
             if(conn != null)conn.close();
         }catch(Exception e) {
             e.printStackTrace();
         }
     }
}
